package com.aida.evil.eda.Window;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.aida.evil.aida.R;
import com.aida.evil.eda.Adapter.CommonAdapter;
import com.aida.evil.eda.Adapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/****************************************************
 * com.aida.evil.eda.PopupWindow.PopupWindows
 * 作者：白雨濃  evil
 * 时间：16-3-9 08:45
 * 邮箱：evilbai@foxmail.com
 * 页面用途：底部弹出窗口
 *           抛出4个抽象函数,用于点击事件
 ****************************************************/
public abstract class PopupWindows extends PopupWindow
{
    private static View view;/*整体控件*/
    private Context context;

    private ListView listView_1;/*上部分list*/
    private ListView listView_2;/*下部分list*/

    private ListAdapter_1 listAdapter_1;/*上部分list适配器*/
    private ListAdapter_2 listAdapter_2;/*下部分list适配器*/

    private ArrayList<ListBean_1> arrayList_1 = new ArrayList<>();
    private ArrayList<ListBean_2> arrayList_2 = new ArrayList<>();

    public PopupWindows(Context context, View parent, String[] list_1, String[] list_2)
    {
        super(context);
        this.context = context;

        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        view = layoutInflater.inflate(R.layout.popwindows, null);


        this.setContentView(view);

        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);

        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);

        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.dialogWindowAnim);

        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);

        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);

        //设置layout在PopupWindow中显示的位置
        this.showAtLocation(parent, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        init(list_1, list_2);

        setListView_1();
        setListView_2();
        setListView_1_OnClick();
        setListView_2_OnClick();
    }

    /**
     * 初始化列表变量
     */
    private void init(String[] list_1, String[] list_2)
    {
        for (String s : list_1)
        {
            arrayList_1.add(new ListBean_1(s));
        }

        for (String s : list_2)
        {
            arrayList_2.add(new ListBean_2(s));
        }
    }

    /**
     * 设置与初始化ListView_1
     */
    private void setListView_1()
    {
        listView_1 = (ListView) view.findViewById(R.id.popwindows_listView1);

        listAdapter_1 = new ListAdapter_1(context, arrayList_1, R.layout.popwindows_listview1_item);
        listView_1.setAdapter(listAdapter_1);

    }

    /**
     * 设置与初始化ListView_2
     */
    private void setListView_2()
    {
        listView_2 = (ListView) view.findViewById(R.id.popwindows_listView2);

        listAdapter_2 = new ListAdapter_2(context, arrayList_2, R.layout.popwindows_listview2_item);
        listView_2.setAdapter(listAdapter_2);

    }


    /**
     * ListView_1的点击事件
     */
    private void setListView_1_OnClick()
    {
        listView_1.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                listView_1_OnClick(parent, view, position, id);
            }
        });
        listView_1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
            {
                listView_1_LongOnClick(parent, view, position, id);
                return true;
            }
        });
    }

    /**
     * ListView_2的点击事件
     */
    private void setListView_2_OnClick()
    {
        listView_2.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                listView_2_OnClick(parent, view, position, id);
            }
        });
        listView_2.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
            {
                listView_2_LongOnClick(parent, view, position, id);
                return true;
            }
        });
    }

    /**
     * 设置是否触动窗口外空白处消失
     *
     * @param isTouchDismiss true为消失
     */
    public void setTouchDismiss(boolean isTouchDismiss)
    {
        if (isTouchDismiss != false)
        {
            view.setOnTouchListener(new View.OnTouchListener()
            {
                public boolean onTouch(View v, MotionEvent event)
                {

                    int height = view.findViewById(R.id.popwindows_linearLayout).getTop();
                    int y = (int) event.getY();
                    if (event.getAction() == MotionEvent.ACTION_UP)
                    {
                        if (y < height)
                        {
                            dismiss();
                        }
                    }
                    return true;
                }
            });
        }
    }

    /**
     * 窗口消失
     */
    public void popupWindowsDismiss()
    {
        dismiss();
    }

    /**
     * 抛出的抽象类
     * ListView_1的短点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    public abstract void listView_1_OnClick(AdapterView<?> parent, View view, int position, long id);

    /**
     * 抛出的抽象类
     * ListView_1的长点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    public abstract void listView_1_LongOnClick(AdapterView<?> parent, View view, int position, long id);

    /**
     * 抛出的抽象类
     * ListView_2的短点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    public abstract void listView_2_OnClick(AdapterView<?> parent, View view, int position, long id);

    /**
     * 抛出的抽象类
     * ListView_2的长点击事件
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    public abstract void listView_2_LongOnClick(AdapterView<?> parent, View view, int position, long id);
    /***************************************
     * 内部类
     * 包含:ListView_1和ListView_2的适配器和Bean
     ***************************************/
    /**
     * ListView的适配器
     */
    private class ListAdapter_1 extends CommonAdapter<ListBean_1>
    {

        public ListAdapter_1(Context context, List<ListBean_1> itemBean_list, int itemLayoutResId)
        {
            super(context, itemBean_list, itemLayoutResId);
        }

        @Override
        public void convert(ViewHolder viewHolder, ListBean_1 item)
        {
            TextView textView = viewHolder.getView(R.id.popwindows_listview1_textView);
            textView.setText(item.text);
        }
    }

    /**
     * ListView的适配器
     */
    private class ListAdapter_2 extends CommonAdapter<ListBean_2>
    {

        public ListAdapter_2(Context context, List<ListBean_2> itemBean_list, int itemLayoutResId)
        {
            super(context, itemBean_list, itemLayoutResId);
        }

        @Override
        public void convert(ViewHolder viewHolder, ListBean_2 item)
        {
            TextView textView = viewHolder.getView(R.id.popwindows_listview2_textView);
            textView.setText(item.text);
        }
    }

    /**
     * JavaBean
     */
    private class ListBean_1
    {
        protected String text;

        public ListBean_1(String text)
        {
            this.text = text;
        }
    }

    /**
     * JavaBean
     */
    private class ListBean_2
    {
        protected String text;

        public ListBean_2(String text)
        {
            this.text = text;
        }
    }
}





















